package com.whu.lsj;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 训练语料库和测试语料的处理
 * @author Enginner-Jie
 */
public class TextProcess {
	/**
	 * @param filepath 待处理语料库文件路径
	 */
	public static void make_train_data(String filepath) {
		File train_file = new File(filepath);
		File crf_train_file = new File(WordSeg.crf_train_file);

		BufferedReader br = null;
		BufferedWriter bw = null;
		try {
			br = new BufferedReader(new FileReader(train_file));
			bw = new BufferedWriter(new FileWriter(crf_train_file));
			String line;
			while ((line = br.readLine()) != null) {
				//根据term标签位置，拆成三部分
				int begin = line.indexOf("<term id=\"");
				int end = line.indexOf("</term>");
				String former = line.substring(0, begin).trim();
				String labelcontent = line.substring(begin, end + 7).trim();
				String later = line.substring(end + 7).trim();

				int a = labelcontent.indexOf("\">");
				int b = labelcontent.lastIndexOf("</term>");
				String terms = labelcontent.substring(a + 2, b).trim();
				//拆分成单词
				String formerwords[] = former.split(" ");
				String termwords[] = terms.split(" ");
				String laterwords[] = later.split(" ");

				//term之前
				if (!former.equals("")) {
					for (int i = 0; i < formerwords.length; i++) {
						bw.write(formerwords[i] + "\tN\n");
					}
				}
				//term内容
				int len = termwords.length;
				if (len == 1) {
					bw.write(termwords[0] + "\tS\n");
				} else if (len == 2) {
					bw.write(termwords[0] + "\tB\n");
					bw.write(termwords[1] + "\tE\n");
				} else if (len >= 2) {
					bw.write(termwords[0] + "\tB\n");
					for (int j = 1; j < len - 1; j++)
						bw.write(termwords[j] + "\tM\n");
					bw.write(termwords[len - 1] + "\tE\n");
				} else {
					bw.write("\n");
				}
				//term之后
				if (!later.equals("")) {
					for (int i = 0; i < laterwords.length; i++) {
						bw.write(laterwords[i] + "\tN\n");
					}
				}
				bw.write("\n");
			}
			System.out.println("----CRF_Train_Text Completed!----");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 读写文件,拿掉term,训练语料转换成测试语料
	 * Created by Enginner-Jie on 2016/8/27.
	 */
	public static void TrainToTest() {
			File test_file = new File(WordSeg.train_raw);
			File crf_test_file = new File(WordSeg.test_raw);

			BufferedReader br = null;
			BufferedWriter bw = null;
			try {
				br = new BufferedReader(new FileReader(test_file));
				bw = new BufferedWriter(new FileWriter(crf_test_file));
				String line;
				while ((line = br.readLine()) != null) {
					bw.write(getSentence(line) + "\n");
				}
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (bw != null){
					try {
						bw.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if (br != null){
					try {
						br.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}

	/**
	 * 获取原始句子，去掉term标签
	 *
	 * @param line
	 */
	public static String getSentence(String line) {

		StringBuffer sb = new StringBuffer(line);

		int t1begin = line.indexOf("<term id=\"");
		int t1end = line.indexOf("\">");

		String part1 = new String(sb.delete(t1begin, t1end + 2));
		String pureSentence = part1.replace("</term>", "");
		//	System.out.println(pureSentence);
		return pureSentence;
	}

	/**
	 * 将测试语料转换为CRF要求的格式：   单词   B(占位符)  一个句子换行
	 * 传入文件编码 UTF-8
	 *
	 * @param filePath 测试语料文件路径
	 */
	static void make_test_data(String filePath) {

		File test_file = new File(filePath);
		File crf_test_file = new File(WordSeg.crf_test_file);

		BufferedReader br = null;
		BufferedWriter bw = null;
		try {
			br = new BufferedReader(new FileReader(test_file));
			bw = new BufferedWriter(new FileWriter(crf_test_file));
			String line;

			while ((line = br.readLine()) != null) {
				String words[] = line.split(" ");  //拆分单词
				for (int i = 0; i < words.length; i++) {
					bw.write(words[i] + "\tB\n");
				}
				bw.write("\n");
			}

			System.out.println("----CRF_Test_Text Completed!----");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

